/*
* Project : Linuxbox
* File    : UserManUpdate.java
* Version : 1.0
* Author  : Kevin Zhang
*
* Copyright (c) 2000-2001 by Linuxbox, Inc.
* All rights reserved.
 */
package linuxbox.admin.servlet;

import javax.servlet.http.*;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.*;
import linuxbox.common.*;

public class UserManUpdateAction extends Action {
  protected static String successJSP = null;
  protected static String check = "CHECKED";

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse res) throws Exception {
    BoxStatic.logger.debug("I am in UserManUpdate now ... ");

    String userID = req.getParameter("USER_ID");
    String password = req.getParameter("PASSWORD");
    String firstName = req.getParameter("FIRST_NAME");
    String lastName = req.getParameter("LAST_NAME");
    String address = req.getParameter("ADDRESS");
    String city = req.getParameter("CITY");
    String state = req.getParameter("STATE");
    String zipcode = req.getParameter("ZIPCODE");
    String phone = req.getParameter("PHONE");
    String fax = req.getParameter("FAX");
    String mobile = req.getParameter("MOBILE");
    String email = req.getParameter("EMAIL");
    String managerID = req.getParameter("MANAGER_ID");
    String deptID = req.getParameter("DEPT_ID");
    String isManager = req.getParameter("IS_MANAGER");
    String note = req.getParameter("NOTE");
    String disable = req.getParameter("DISABLE");
    String noExpire = req.getParameter("NO_EXPIRE");
    String systemAdmin = req.getParameter("SYSTEM_ADMIN");

    String newsAdmin = req.getParameter("NEWS_ADMIN");
    String todoAdmin = req.getParameter("TODO_ADMIN");
    String pollAdmin = req.getParameter("POLL_ADMIN");
    String resourceAdmin = req.getParameter("RESOURCE_ADMIN");
    String reservAdmin = req.getParameter("RESERV_ADMIN");
    String downloadAdmin = req.getParameter("DOWNLOAD_ADMIN");
    String bbsAdmin = req.getParameter("BBS_ADMIN");
    String userAdmin = req.getParameter("USER_ADMIN");
    String bookmarkAdmin = req.getParameter("BOOKMARK_ADMIN");

    BoxStatic.logger.debug("userID is " + userID);
    BoxStatic.logger.debug("password is " + password);
    BoxStatic.logger.debug("firstName is " + firstName);
    BoxStatic.logger.debug("lastName is " + lastName);
    BoxStatic.logger.debug("note is " + note);
    BoxStatic.logger.debug("disable is " + disable);
    BoxStatic.logger.debug("noExpire is " + noExpire);

    UserManager um = UserManager.getInstance();
    User user = um.getUser(Integer.parseInt(userID));

    long right1 = user.getUserRights();
    BoxStatic.logger.debug("User rights is " + Long.toHexString(right1));

    if (check.equals(systemAdmin)) {
      right1 = UserRights.setSystemAdmin(right1);
    } else {
      right1 = UserRights.unsetSystemAdmin(right1);
    }

    if (check.equals(newsAdmin)) {
      right1 = UserRights.setNewsAdmin(right1);
    } else {
      right1 = UserRights.unsetNewsAdmin(right1);
    }

    if (check.equals(todoAdmin)) {
      right1 = UserRights.setTodoAdmin(right1);
    } else {
      right1 = UserRights.unsetTodoAdmin(right1);
    }

    if (check.equals(pollAdmin)) {
      right1 = UserRights.setPollAdmin(right1);
    } else {
      right1 = UserRights.unsetPollAdmin(right1);
    }

    if (check.equals(resourceAdmin)) {
      right1 = UserRights.setResourceAdmin(right1);
    } else {
      right1 = UserRights.unsetResourceAdmin(right1);
    }

    if (check.equals(reservAdmin)) {
      right1 = UserRights.setReservationAdmin(right1);
    } else {
      right1 = UserRights.unsetReservationAdmin(right1);
    }

    if (check.equals(downloadAdmin)) {
      right1 = UserRights.setDownloadAdmin(right1);
    } else {
      right1 = UserRights.unsetDownloadAdmin(right1);
    }

    if (check.equals(bbsAdmin)) {
      right1 = UserRights.setBBSAdmin(right1);
    } else {
      right1 = UserRights.unsetBBSAdmin(right1);
    }

    if (check.equals(userAdmin)) {
      right1 = UserRights.setUserAdmin(right1);
    } else {
      right1 = UserRights.unsetUserAdmin(right1);
    }

    if (check.equals(bookmarkAdmin)) {
      right1 = UserRights.setBookmarkAdmin(right1);
    } else {
      right1 = UserRights.unsetBookmarkAdmin(right1);
    }

    BoxStatic.logger.debug("User rights is " + Long.toHexString(right1));

    user = new User(Integer.parseInt(userID), null, password, firstName,
                    lastName, right1, -1, check.equals(noExpire));

    user.setAddress(address);
    user.setCity(city);
    user.setState(state);
    user.setZipCode(zipcode);
    user.setPhone(phone);
    user.setFax(fax);
    user.setMobile(mobile);
    user.setEmail(email);

    if (managerID != null && !"".equals(managerID))
      user.setManagerID(Integer.parseInt(managerID));

    if (deptID != null && !"".equals(deptID))
      user.setDeptID(Integer.parseInt(deptID));

    user.setIsManager(check.equals(isManager));
    user.setDisable(check.equals(disable));
    user.setNote(note);

    um.modifyUser(user);

    redirectFile(successJSP, req, res);
    BoxStatic.logger.debug("I am leaving UserManUpdate now ... ");
  }

  public void setSuccessJSP(String str) {
    //"/linuxbox/jsp/admin/UserManager.jsp"
    successJSP = str;
  }
}